<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * Getphone Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */
class Getphone extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		if($this->database_connect_status){
			$this->set_controller('getphone');
                        $this->load->model('getphone_model');
                        $this->load->model('booking_model');
                        $this->load->model('sales_model');
		}
                
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        public function lists(){
            $this->view_data['lists']  = $this->getphone_model->selectAll($this->input->get('term',null));
            $this->load->view('auth/getphone/lists', $this->view_data);
        }
        
        public function existsphone(){
            $params                             = $this->input->get('term',null);
            $phone                              = $this->getphone_model->existsphone($params);
            if($phone){
                die("0");//3 :SDT da ton tai trong list KH
            }
            $_phone                             = $this->sales_model->find_by(array("phone" => $params));
            $data = array(
                    'name'          =>  (isset($_phone[0]->name))?$_phone[0]->name:'',
                    'type'          =>  (isset($_phone[0]->type))?$_phone[0]->type:'',
                    'address'       =>  (isset($_phone[0]->address))?$_phone[0]->address:'',
                    'province'      =>  (isset($_phone[0]->province)&&$_phone[0]->province!=0)?$_phone[0]->province:'27',
                    'email'         =>  (isset($_phone[0]->email))?$_phone[0]->email:'',
                    'salesID'       =>  (isset($_phone[0]->owner))?$_phone[0]->owner:''
            );

            $data = json_encode($data);
            die($data);
        }
        
        public function getlists(){
            
            if($this->input->server('REQUEST_METHOD')=='POST'){   
                $params = array(
                    'airline'             =>  strtoupper($this->input->post('airline',null)),//hãng
                    'controlBooking'      =>  $this->input->post('controlBooking',null)//source
                );

                switch($params['airline'])
                {
                        case 'VJ':
                            $result = $this->bookinglib->controlVJ($params['controlBooking']);
                             break;
                        case 'TR':
                            $result = $this->bookinglib->controlTR($params['controlBooking']);
                             break;
                        case 'JT':
                            $result = $this->bookinglib->controlJT($params['controlBooking']);
                             break;
                        case 'BL':
                            $result = $this->bookinglib->controlBL($params['controlBooking']);
                             break;
                        case 'AK':
                            $result = $this->bookinglib->controlAK($params['controlBooking']);
                             break;
                }

                die(json_encode($result));
            } else {
                redirect(basr_url());
            }
        }
        
        public function infoBooking(){
            $this->view_data['id'] = $this->input->get('id',null);
            $this->load->view('auth/getphone/infoBooking', $this->view_data);
        }
}